package editAir
{
	import _Pan3D.base.ObjData;
	
	import _me.Config;
	import _me.Scene_data;
	
	import com.adobe.utils.AGALMiniAssembler;
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;

	// --------------MSN:lation_pan@live.cn  QQ: 3423526------------- //
	[SWF(backgroundColor="#FFFFFF", frameRate="60", width="1024", height="600")]
	public class testOrtographicCam extends MovieClip
	{
		private var box:ObjData;
		private var _program3D:Program3D;
		[Embed(source="../_File/beijing1/panpanpan.jpg")]
		private static var newDengGuang:Class;
		private var _projMatrix:PerspectiveMatrix3D;

		public function testOrtographicCam()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addToStage)
		}

		protected function addToStage(event:Event):void
		{
			Config.init(stage, _config_end);
		}
		

		private function _config_end():void
		{
			Scene_data.stage3D.x=(1024 - 600) / 2
			Scene_data.context3D.configureBackBuffer(600, 600, 1, true);
			Scene_data.context3D.setCulling(Context3DTriangleFace.NONE);

			box=new ObjData;
			makeModelData();
			makeProgram3D();
			makeShenduText();
			Scene_data.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame)
			Scene_data.stage.addEventListener(MouseEvent.MOUSE_WHEEL, _onMouseWheel)

		}
		private var _num:Number=0;

		private function _onMouseWheel(event:MouseEvent):void
		{
			_num=_num + (event.delta / 1)
		}

		private function onEnterFrame(event:Event):void
		{
			upData();
		}

		private function upData():void
		{
			var _context3D:Context3D=Scene_data.context3D;
			_context3D.clear(0, 0, 1, 1);
			_context3D.setProgram(_program3D);
			
			var mvp:Matrix3D=new Matrix3D;
			mvp.copyRawDataFrom(Vector.<Number>([
				1,0,0,0,
				0,1,0,0,
				0,0,1,0,
				0,0,0,1]
			));
			_context3D.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 0, Vector.<Number>([0, 0, 0.0001, 0]));
			var modeMatrix3D:Matrix3D=new Matrix3D();
			modeMatrix3D.prependRotation(_num,Vector3D.Y_AXIS)
			modeMatrix3D.appendTranslation(0,0,10)
			_context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,4,modeMatrix3D);
			
			
			_context3D.setVertexBufferAt(0, box.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context3D.setVertexBufferAt(1, box.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context3D.setTextureAt(0, box.texture);
			_context3D.drawTriangles(box.indexBuffer, 0, -1);
			_context3D.present();


		}

		private function makeShenduText():void
		{
			var _context3D:Context3D=Scene_data.context3D
			var tempBmp:BitmapData=Bitmap(new newDengGuang()).bitmapData;
			box.texture=_context3D.createTexture(tempBmp.width, tempBmp.height, Context3DTextureFormat.BGRA, true);
			box.texture.uploadFromBitmapData(tempBmp);

		}

		private function makeProgram3D():void
		{
			var _context3D:Context3D=Scene_data.context3D
			var _vertexShaderAssembler:AGALMiniAssembler=new AGALMiniAssembler();
			_vertexShaderAssembler.assemble(Context3DProgramType.VERTEX,
                
				"m44 vt0,va0,vc4 \n"+
				"add vt0.x,vt0.x,vc0.x \n"+
				"mov v1,vt0 \n"+
				"mul vt0.z,vt0.z,vc0.z \n"+
				"mov op, vt0 \n"+
				"mov v0, va1 \n"

				);
			var _fragmentShaderAssembler:AGALMiniAssembler=new AGALMiniAssembler();
			_fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT, 
				"tex ft0, v0, fs0 <2d,clamp,linear>\n" +
				
				"mov oc, ft0"
			);
			_program3D=_context3D.createProgram();
			_program3D.upload(_vertexShaderAssembler.agalcode, _fragmentShaderAssembler.agalcode)
		}

		private function makeModelData():void
		{
			var _context3D:Context3D=Scene_data.context3D
			var vertices:Array=new Array;
		/*	vertices.push(-2, 1, 0.5);
			vertices.push(-2, -1, 0.5);
			vertices.push(0, -1, 0.5);
			vertices.push(0, 1, 0.5);
*/
			
			vertices.push(-0.6, 0.6, 1);
			vertices.push(-0.6, -0.5, 1);
			vertices.push(0.5, -0.5, 1);
			vertices.push(0.5, 0.5, 1);
			
			vertices.push(-0, 0.5, 10);
			vertices.push(-0, -0.5, 10);
			vertices.push(0.5, -0.5, 10);
			vertices.push(1, 1, 10);



			box.vertexBuffer=_context3D.createVertexBuffer(vertices.length / 3, 3);
			box.vertexBuffer.uploadFromVector(Vector.<Number>(vertices), 0, vertices.length / 3);
			var uvs:Array=new Array;
			uvs.push(0, 0);
			uvs.push(0, 0.5);
			uvs.push(0.5, 0.5);
			uvs.push(0.5, 0);
			
			uvs.push(0.5, 0.5);
			uvs.push(0.5, 1);
			uvs.push(1, 1);
			uvs.push(1, 0.5);
			
			box.uvBuffer=_context3D.createVertexBuffer(uvs.length / 2, 2);
			box.uvBuffer.uploadFromVector(Vector.<Number>(uvs), 0, uvs.length / 2);
			var indexs:Array=new Array;
			indexs.push(0, 1, 2);
			indexs.push(5, 6, 7);
			box.indexBuffer=_context3D.createIndexBuffer(indexs.length);
			box.indexBuffer.uploadFromVector(Vector.<uint>(indexs), 0, indexs.length);

		}

	}
}
